package com.fc.v2.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.fc.v2.model.auto.TSysDatas;
import com.fc.v2.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.v2.common.base.BaseService;
import com.fc.v2.common.support.ConvertUtil;
import com.fc.v2.mapper.auto.ItemDataMapper;
import com.fc.v2.model.auto.ItemData;
import com.fc.v2.model.auto.ItemDataExample;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.util.SnowflakeIdWorker;
import com.fc.v2.util.StringUtils;

/**
 * 主体和图片映射关系 ItemDataService
 * @Title: ItemDataService.java 
 * @Package com.fc.v2.service 
 * @author fuce_自动生成
 * @email ${email}
 * @date 2021-02-25 09:34:32  
 **/
@Service
public class ItemDataService implements BaseService<ItemData, ItemDataExample>{
	@Autowired
	private ItemDataMapper itemDataMapper;
	@Autowired
	private TSysDatasService sysDatasService;

	/**
	 * 获取一个item的封面
	 * @param itemId
	 * @return
	 */
	public String findCover(String itemId){
		TSysDatas firstData = findFirstData(itemId);
		if(firstData == null){
			return null;
		}
		return firstData.getFileName();
	}
	/**
	 * 获取item的第一个图片
	 * @param itemId
	 * @return
	 */
	public TSysDatas findFirstData(String itemId){
		ItemDataExample e = new ItemDataExample();
		e.createCriteria().andItemIdEqualTo(itemId);
		List<ItemData> list = selectByExample(e);
		if(list.size()>0){
			ItemData d = list.get(0);
			TSysDatas tSysDatas = sysDatasService.selectByPrimaryKey(d.getDataId());
			return tSysDatas;
		}
		return null;

	}


	/**
	 * 获取某个itemId下的所有文件
	 * @param itemId
	 * @return
	 */
	public List<TSysDatas> findDatas(String itemId){
		ItemDataExample e = new ItemDataExample();
		e.createCriteria().andItemIdEqualTo(itemId);
		List<ItemData> list = selectByExample(e);
		List<TSysDatas> res= new ArrayList<>();
		for(ItemData d:list){
			TSysDatas tSysDatas = sysDatasService.selectByPrimaryKey(d.getDataId());
			res.add(tSysDatas);
		}
		return res;

	}

	/**
	 * 获取itemId的所有文件名
	 * @param itemId
	 * @return
	 */
	public List<String> findAllFileNames(String itemId){
		List<TSysDatas> datas = findDatas(itemId);
		ArrayList<String> names = new ArrayList<>();
		for(TSysDatas t:datas){
			names.add(t.getFileName());
		}
		return names;
	}

      	   	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<ItemData> list(Tablepar tablepar,ItemData itemData){
	        ItemDataExample testExample=new ItemDataExample();
			//搜索
			if(StrUtil.isNotEmpty(tablepar.getSearchText())) {//小窗体
	        	testExample.createCriteria().andLikeQuery2(tablepar.getSearchText());
	        }else {//大搜索
	        	testExample.createCriteria().andLikeQuery(itemData);
	        }
			//表格排序
			//if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
	        //	testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
	        //}else{
	        //	testExample.setOrderByClause("id ASC");
	        //}
	        PageHelper.startPage(tablepar.getPage(), tablepar.getLimit());
	        List<ItemData> list= itemDataMapper.selectByExample(testExample);
	        PageInfo<ItemData> pageInfo = new PageInfo<ItemData>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			ItemDataExample example=new ItemDataExample();
			example.createCriteria().andIdIn(stringB);
			return itemDataMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public ItemData selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return itemDataMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(ItemData record) {
		return itemDataMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(ItemData record) {
				
		record.setId(null);
		record.setSubmitTime(DateUtils.getNowDate());
		
				
		return itemDataMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(ItemData record, ItemDataExample example) {
		
		return itemDataMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(ItemData record, ItemDataExample example) {
		
		return itemDataMapper.updateByExample(record, example);
	}

	@Override
	public List<ItemData> selectByExample(ItemDataExample example) {
		
		return itemDataMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(ItemDataExample example) {
		
		return itemDataMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(ItemDataExample example) {
		
		return itemDataMapper.deleteByExample(example);
	}


}
